import logging
import config
import asyncio
from service import MysqlService, CrawlService

crawl_service = CrawlService()
sem = asyncio.Semaphore(config.SEM)


def create_table():
    """创建数据库"""
    if MysqlService.create_table() == -1:
        exit()


def crawl_store_city():
    """抓取所有城市信息，保存到数据库"""
    item_list = None
    try:
        item_list = crawl_service.crawl_citys()
    except Exception as e:
        print(f"爬取城市数据出错，错误：{e}")
        exit()
    if not item_list:
        print("解析的城市信息为空，程序即将退出！")
        exit()
    print("开始将所有城市名和链接存入数据库。。。")
    for item in item_list:
        if MysqlService.insert_data(config.CITY_TABLE, item) == -1:
            exit()


async def crawl_store_room(city_data):
    """抓取二手房的信息"""
    if city_data[3] != config.STATUS_NOT_CRAWL:
        exit()
    for cont in range(1, 2):
        page_url = f"{city_data[2]}" + f"pg{cont}"
        page_room_data = await crawl_service.crawl_page(page_url)
        for page_data in page_room_data[:1]:
            room_data = await crawl_service.crawl_room(page_data)
            print(room_data)
            if not room_data:
                print("获取二手房信息为空")
            MysqlService.insert_data(config.ROOM_TABLE, room_data)


async def main():
    city_tuples = MysqlService.query_data(config.CITY_TABLE)
    # city_tuples = [["https://aq.lianjia.com/ershoufang/"]]
    if not city_tuples:
        exit()
    task = []
    for city_data in city_tuples[:1]:
        try:
            print(city_data[2])
            task.append(asyncio.create_task(crawl_store_room(city_data)))
        except Exception as e:
            print(f"获取二手房信息出误，错误：{e}")
    await asyncio.wait(task)


def run():
    create_table()          # 创建数据库
    crawl_store_city()      # 抓取所有城市信息
    asyncio.run(main())        # 抓取二手房的信息


if __name__ == '__main__':
    run()
    # city_tuples = MysqlService.query_data(config.CITY_TABLE)
    # print(city_tuples)



